﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace Surveillance
{
    public class Keypad
    {

        #region keypad declarations

        public static Texture2D padTexture;
        public static Texture2D boxTexture;
        private Rectangle targetLocation;
        private Rectangle frameSource;
        private Vector2 textPosition;
        private string password;
        private string currentString;
        private Enemy ApprovedEnemy;
        private bool active;
        private float timePassed;
        private float keyTimePassed;
        private float enemyDelay;
        private Door linkedDoor;
        private bool enemyClose;
        private int indexOfNext;

        #endregion

        #region keypad constructors

        public Keypad(Rectangle target, string pass, Enemy enemy, Door door)
        {
            targetLocation = target;
            textPosition = new Vector2(target.X, target.Y - 20);
            frameSource = new Rectangle(0,0,20,20);
            password = pass;
            currentString = "";
            ApprovedEnemy = enemy;
            enemyClose = false;
            active = false;
            timePassed = 0;
            keyTimePassed = 0;
            enemyDelay = 0;
            linkedDoor = door;
            indexOfNext = 0;
        }

        #endregion

        #region keypad update and draw

        public void Update(GameTime gameTime)
        {
            float elapsed = gameTime.ElapsedGameTime.Milliseconds;
            enemyDelay += elapsed;
            if (Math.Abs(ApprovedEnemy.TargetRectangle.X - targetLocation.X) <= 200 && enemyDelay > 3000)
            {
                //enemy is near the keypad, open the door
                enemyDelay = 0;
                enemyClose = true;
            }
            if (enemyClose && !active)
            {
               //add a letter
               addLetter(gameTime); 
            }
            else if (active)
            {
                readFromKeyboard(gameTime);
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(padTexture, targetLocation, frameSource, Color.White);
            spriteBatch.DrawString(Game1.font,currentString,textPosition,Color.Black,0f,Vector2.Zero,0.5f,SpriteEffects.None,0f);
        }

        public void DrawTextBox(SpriteBatch spriteBatch)
        {
            if (active)
            {
                spriteBatch.DrawString(Game1.font, "Enter Password: (press Enter to confirm)",new Vector2(200, 170), Color.Black);
                spriteBatch.Draw(boxTexture,new Rectangle(200,200,400,50),Color.White);
                spriteBatch.DrawString(Game1.font, currentString, new Vector2(200, 200), Color.Black);
            }
        }

        #endregion

        #region keypad auxiliary

        private void addLetter(GameTime gameTime)
        {
            float elapsed = gameTime.ElapsedGameTime.Milliseconds;
            timePassed += elapsed;
            if (timePassed < 400)
            {
                return;
            }
            timePassed = 0;
            if (Math.Abs(ApprovedEnemy.TargetRectangle.X - targetLocation.X) > 200)
            {
                indexOfNext = 0;
                currentString = "";
                enemyClose = false;
                return;
            }
            if (indexOfNext >= password.Length)
            {
                //whole password is entered
                indexOfNext = 0;
                enemyClose = false;
                enterPassword();
            }
            else
            {
                currentString += password.ElementAt(indexOfNext);
                indexOfNext++;
            }
        }

        public void enterPassword()
        {
            Console.WriteLine("real: " + password + "\n try: " + currentString);
            if (currentString.Equals(password))
            {
                linkedDoor.doSwitch(0);
            }
            currentString = "";
            timePassed = -3000;
            enemyClose = false;
        }

        public void mark()
        {
            frameSource.X = 20;
        }

        public void unmark()
        {
            frameSource.X = 0;
        }

        public void click()
        {
            if (!active)
            {
                active = true;
                Game1.activeTextBox = true;
                timePassed = 0;
                currentString = "";
            }
            else
            {
                active = false;
                Game1.activeTextBox = false;
            }
        }

        public void reset()
        {
            currentString = "";
            frameSource.X = 0;
            enemyClose = false;
            active = false;
            Game1.activeTextBox = false;
            timePassed = 0;
            keyTimePassed = 0;
            enemyDelay = 0;
            indexOfNext = 0;
            Game1.activeTextBox = false;
        }

        public void readFromKeyboard(GameTime gameTime)
        {
            KeyboardState keyState = Keyboard.GetState();
            float elapsed = gameTime.ElapsedGameTime.Milliseconds;
            keyTimePassed += elapsed;
            if (keyTimePassed < 100)
            {
                return;
            }
            keyTimePassed = 0;
            if (keyState.IsKeyDown(Keys.A))
            {
                currentString += "a";
            }
            else if (keyState.IsKeyDown(Keys.B))
            {
                currentString += "b";
            }
            else if (keyState.IsKeyDown(Keys.C))
            {
                currentString += "c";
            }
            else if (keyState.IsKeyDown(Keys.D))
            {
                currentString += "d";
            }
            else if (keyState.IsKeyDown(Keys.E))
            {
                currentString += "e";
            }
            else if (keyState.IsKeyDown(Keys.F))
            {
                currentString += "f";
            }
            else if (keyState.IsKeyDown(Keys.G))
            {
                currentString += "g";
            }
            else if (keyState.IsKeyDown(Keys.H))
            {
                currentString += "h";
            }
            else if (keyState.IsKeyDown(Keys.I))
            {
                currentString += "i";
            }
            else if (keyState.IsKeyDown(Keys.J))
            {
                currentString += "j";
            }
            else if (keyState.IsKeyDown(Keys.K))
            {
                currentString += "k";
            }
            else if (keyState.IsKeyDown(Keys.L))
            {
                currentString += "l";
            }
            else if (keyState.IsKeyDown(Keys.M))
            {
                currentString += "m";
            }
            else if (keyState.IsKeyDown(Keys.N))
            {
                currentString += "n";
            }
            else if (keyState.IsKeyDown(Keys.O))
            {
                currentString += "o";
            }
            else if (keyState.IsKeyDown(Keys.P))
            {
                currentString += "p";
            }
            else if (keyState.IsKeyDown(Keys.Q))
            {
                currentString += "q";
            }
            else if (keyState.IsKeyDown(Keys.R))
            {
                currentString += "r";
            }
            else if (keyState.IsKeyDown(Keys.S))
            {
                currentString += "s";
            }
            else if (keyState.IsKeyDown(Keys.T))
            {
                currentString += "t";
            }
            else if (keyState.IsKeyDown(Keys.U))
            {
                currentString += "u";
            }
            else if (keyState.IsKeyDown(Keys.V))
            {
                currentString += "v";
            }
            else if (keyState.IsKeyDown(Keys.W))
            {
                currentString += "w";
            }
            else if (keyState.IsKeyDown(Keys.X))
            {
                currentString += "x";
            }
            else if (keyState.IsKeyDown(Keys.Y))
            {
                currentString += "y";
            }
            else if (keyState.IsKeyDown(Keys.Z))
            {
                currentString += "z";
            }
            else if (keyState.IsKeyDown(Keys.Back))
            {
                if (currentString.Length > 0)
                {
                    currentString = currentString.Substring(0, currentString.Length - 1);
                }
            }
            else if (keyState.IsKeyDown(Keys.Space))
            {
                currentString += " ";
            }
            else if (keyState.IsKeyDown(Keys.Enter))
            {
                enterPassword();
                active = false;
                Game1.activeTextBox = false;
            }
            else if (keyState.IsKeyDown(Keys.Escape))
            {
                currentString = "";
                active = false;
                Game1.activeTextBox = false;
            }
        }

        #endregion

        #region keypad properties

        public Rectangle TargetLocation
        {
            get { return targetLocation; }
            set { targetLocation = value; }
        }

        public bool Active
        {
            get { return active; }
            set { active = value; }
        }

        #endregion
    }
}
